package com.shzg.im.controller;

import java.sql.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.shzg.im.model.ApiResult;
import com.shzg.im.model.Message;
import com.shzg.im.model.User;
import com.shzg.im.service.MessageService;
import com.shzg.im.web.binder.CustomLongDateEditor;

@RestController
@RequestMapping("/msg")
@SessionAttributes("user")
public class MessageController {
    @Autowired
    private MessageService messgeService;
    private SimpMessagingTemplate template;

    @Autowired
    public MessageController(SimpMessagingTemplate t) {
        this.template = t;
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ApiResult sendMessage(@ModelAttribute("SpringWeb") Message msg) {
        ApiResult apiRst = new ApiResult();

        messgeService.saveMsg(msg);

        System.out.println(msg);

        return apiRst;
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ApiResult getMessage(@ModelAttribute User user) {
        ApiResult apiRst = new ApiResult();

        List<Map<String, Object>> rstMsgLst = new LinkedList<Map<String, Object>>();
        List<Message> msgLst = messgeService.getMessageListByDestId(user
                .getLogin_id());

        for (Message msg : msgLst) {

            Map<String, Object> msgInfoMap = new LinkedHashMap<String, Object>();

            msgInfoMap.put("fromUserId", msg.getFrom_user_id());
            msgInfoMap.put("messageContent", msg.getMessage_content());
            msgInfoMap.put("readFlag", msg.getRead_flag());
            msgInfoMap.put("messageType", msg.getMessage_type());
            msgInfoMap.put("sendTime", msg.getSend_time().getTime());

            rstMsgLst.add(msgInfoMap);

        }

        apiRst.setObj(rstMsgLst);
        return apiRst;
    }

    @MessageMapping("/queue")
    public void userChat(ApiResult apiRst, Message msg) {
        messgeService.saveMsg(msg);

        List<Map<String, Object>> rstMsgLst = new LinkedList<Map<String, Object>>();
        Map<String, Object> msgInfoMap = new LinkedHashMap<String, Object>();

        msgInfoMap.put("fromUserId", msg.getFrom_user_id());
        msgInfoMap.put("messageContent", msg.getMessage_content());
        msgInfoMap.put("readFlag", msg.getRead_flag());
        msgInfoMap.put("messageType", msg.getMessage_type());
        msgInfoMap.put("sendTime", msg.getSend_time().getTime());
        rstMsgLst.add(msgInfoMap);

        apiRst.setObj(rstMsgLst);

        // 消息将会发送到目标帐号订阅通道
        String dest = "queue/msg" + msg.getTo_dest_id();
        this.template.convertAndSend(dest, apiRst);
    }

    @SubscribeMapping("/chatlog/{userId}")
    public ApiResult initChatlog(@DestinationVariable String userId) {
        ApiResult apiRst = new ApiResult();

        List<Map<String, Object>> rstMsgLst = new LinkedList<Map<String, Object>>();
        List<Message> msgLst = messgeService.getMessageListByDestId(userId);

        for (Message msg : msgLst) {

            Map<String, Object> msgInfoMap = new LinkedHashMap<String, Object>();

            msgInfoMap.put("fromUserId", msg.getFrom_user_id());
            msgInfoMap.put("messageContent", msg.getMessage_content());
            msgInfoMap.put("readFlag", msg.getRead_flag());
            msgInfoMap.put("messageType", msg.getMessage_type());
            msgInfoMap.put("sendTime", msg.getSend_time().getTime());

            rstMsgLst.add(msgInfoMap);

        }

        apiRst.setObj(rstMsgLst);
        return apiRst;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomLongDateEditor());
    }

}
